package com.netease.utils;

import com.netease.entity.IndexDictionary;
import com.netease.entity.Word;

import java.io.*;
import java.util.*;

/**
 * text2 csv
 *
 * @author aochong
 * @date 2022/12/13
 */
public class FileUtils {
    /**
     * array2 csv
     * 导出到csv文件
     *
     * @param data 数据
     * @param path 路径
     * @param num  全国矿工工会
     */
    public static void Array2CSV(ArrayList<ArrayList<String>> data, String path, int num) {
        try {
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), "UTF-8"));
            for (int i = 0; i < data.size(); i++) {
                ArrayList<String> onerow = data.get(i);
                //ArrayList<String> onerow=data.get(i);
                for (int j = 0; j < onerow.size(); j++) {
                    out.write(DelQuota(onerow.get(j)));
                    out.write(",");
                }
                out.newLine();
            }
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * ArrayList集合对象写CSV文件
     * @param data
     * @param path
     */
    public static void ArrayList2CSV(ArrayList<ArrayList<Word>> data, String path) {
        try {
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), "UTF-8"));
            for (int i = 0; i < data.size(); i++) {
                ArrayList<Word> onerow = data.get(i);
                for (int j = 0; j < onerow.size(); j++) {
                    out.write(DelQuota(onerow.get(j).getName()) + "," + onerow.get(j).getEngName() + "," + onerow.get(j).getEngAbbreviation());
                    if (j <= onerow.size() - 1){
                        out.newLine();
                    }
                }
            }
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 配额
     *
     * @param str str
     * @return {@link String}
     */
    public static String DelQuota(String str) {
        String result = str;
        String[] strQuota = {"~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "`", ";", "'", ",", ".", "/", ":", "/,", "<", ">", "?", "_"};
        for (int i = 0; i < strQuota.length; i++) {
            if (result.indexOf(strQuota[i]) > -1) {
                result = result.replace(strQuota[i], "");
            }
        }
        return result;
    }

    /**
     * csv2数组
     * 从csv文件中读取数据
     *
     * @param path 路径
     * @return {@link ArrayList}<{@link ArrayList}<{@link String}>>
     */
    public static ArrayList<ArrayList<String>> CSV2Array(String path) {
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
            ArrayList<ArrayList<String>> alldata = new ArrayList<ArrayList<String>>();
            String line;
            String[] onerow;
            while ((line = in.readLine()) != null) {
                //默认分割符为逗号，可以不使用逗号
                onerow = line.split(",");
                List<String> onerowlist = Arrays.asList(onerow);
                ArrayList<String> onerowaArrayList = new ArrayList<String>(onerowlist);
                alldata.add(onerowaArrayList);
            }
            in.close();
            return alldata;
        } catch (Exception e) {
            return null;
        }

    }

    /**
     * CSV文件转IndexDictionary对象
     * @param path
     * @return
     */
    public static ArrayList<IndexDictionary> CSV2IndexDictionary(String path) {
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
            ArrayList<IndexDictionary> alldata = new ArrayList<>();
            String line;
            while ((line = in.readLine()) != null) {
                IndexDictionary indexDictionary = new IndexDictionary();
                indexDictionary.setIndexName(line);
                alldata.add(indexDictionary);
            }
            return alldata;
        } catch (Exception e) {
            try {
                System.out.println(in.readLine());
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            return null;
        }finally {
            if (in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    /**
     * CSV文件转Map字典对象
     * @param path
     * @return
     */
    public static Map<String,String> Csv2MapDictionary(String path){
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
            HashMap<String, String> mapDictionary = new HashMap<>();
            String line;
            while ((line = in.readLine()) != null) {
                String[] split = line.split(",");
                mapDictionary.put(split[0],split[1]);
            }
            return mapDictionary;
        } catch (Exception e) {
            try {
                System.out.println("Csv2MapDictionary 出现异常：" + in.readLine());
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            return null;
        }finally {
            if (in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    public static void writeFile(List<String> lines,String path){
        BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), "UTF-8"));
            for (int i = 0; i < lines.size(); i++) {
                bufferedWriter.write(lines.get(i));
                if (i < lines.size()) {
                    bufferedWriter.newLine();
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try{
                if (bufferedWriter != null) {
                    bufferedWriter.close();
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
